<?php
/***
 * 
 * @author GuoLong
 * 2014/12/03
 * 
 */
class Questionoranswer extends Model {
	
	private $table="xdf_questionoranswer";

	private $fields=array("id","user_id","target_id","video_id","content_type","content_id","create_time");
	
	//添加提问或问题
	public function addQA($user_id,$target_id,$video_id,$content_type,$content_id) {
		$sql="INSERT INTO `".$this->table."` 
		(`user_id`,`target_id`,`video_id`,`content_type`,`content_id`,`create_time`) 
		VALUES ('$user_id','$target_id','$video_id','$content_type','$content_id',now());";

		return $this->_db->insert($sql);
	}
	//删除我的提问
	public function delMyQA($qa_id,$user_id){
		$sql="DELETE FROM `".$this->table."` WHERE `id`='".$qa_id."' and `user_id`= '".$user_id."'";
		return $this->_db->del($sql);
	}
	//获取我的提问总数
	public function getMyQANum($user_id){
		return $this->_db->getTotal($this->table,"user_id='$user_id'");
	}
	//获取提问总数
	public function getQANum(){
		return $this->_db->getTotal($this->table);
	} 
	//获取问题和回答详情
	public function getQAInfo($qa_id){
		$sql=" select
		q.`id` as qa_id,
		q.`user_id` as `user_id`,
		u.`name` as `user_name`,
		q.`target_id` as target_id,
		uu.`name` as `target_name`,
		q.`create_time` as `create_time`,
		q.content_type as `content_type`,
		q.content_id as content_id
		FROM {$this->table} q
		LEFT JOIN `xdf_user` u ON u.id=q.user_id
		LEFT JOIN {$this->table} qq ON q.target_id = qq.id
		LEFT JOIN `xdf_user` uu on uu.id= qq.user_id
		WHERE q.`id` = '{$qa_id}';";
		$data= $this->fetchRow( $sql );
		return $data;
	}
	//获取id为video_id的问答列表
	public function getQAList($video_id,$timestamp,$pagesize,$action){

// 		if($action=='pre')//刷新
			$preOrMore_sql="";
		if($action=='more'){//获取更多
			$preOrMore_sql="AND q.id < $timestamp";
		}
		
		$sql=" select
				q.`id` as qa_id,  
				q.`user_id` as `user_id`,
				u.`name` as `user_name`,
				q.`target_id` as target_id,
				uu.`name` as `target_name`,
				q.`create_time` as `create_time`,
				q.content_type as `content_type`,
				q.content_id as content_id
				FROM {$this->table} q
				LEFT JOIN `xdf_user` u ON u.id=q.user_id
				LEFT JOIN {$this->table} qq ON q.target_id = qq.id
				LEFT JOIN `xdf_user` uu on uu.id= qq.user_id
				WHERE q.`video_id` = '{$video_id}' ".$preOrMore_sql." ORDER BY q.id DESC LIMIT 0,$pagesize;";

// 		var_dump($sql);
		$data= $this->fetchAll( $sql );
		return $data;
	}
	
	public function getQANumByVideo($video_id){
		return $this->_db->getTotal($this->table,"video_id='$video_id'");
	}
	
	/**
	 * 获取消息发送的对象
	 */
	public function getUserId($target_id){
		$sql = "SELECT user_id FROM `$this->table` WHERE id = $target_id";
		return $this->fetchRow( $sql );
	}
}